package com.atguigu.gulimall.order.web;

import com.atguigu.common.constant.OrderConstant;
import com.atguigu.gulimall.order.config.OrderRabbitMQConfig;
import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.atguigu.gulimall.order.vo.OrderConfirmVo;
import com.atguigu.gulimall.order.vo.OrderSubmitRequestVo;
import com.atguigu.gulimall.order.vo.OrderSubmitResponseVo;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@Slf4j
public class PageController {

    @Autowired
    private OrderService orderService;

    @RequestMapping(path = {"/{page}.html"})
    public String page(@PathVariable("page") String page) {
        return page;
    }


    @RequestMapping(path = {"/toTrade"})
    public String toTrade(Model model // ,
//                          @ModelAttribute("message") String message,
//                          @RequestParam(value = "name",required = false) String name
    ) {
        OrderConfirmVo vo = orderService.confirmOrder();

//        System.out.println("toTrade 方法");

        model.addAttribute("vo", vo);
//        if (session.getAttribute("msg") != null) {
//            model.addAttribute("msg", session.getAttribute("msg"));
//        }
//        if (msg != null) {
//            model.addAttribute("msg", msg);
//        }
//        log.info("message is {}, name is {}", message, name);
//        model.addAttribute("msg", message);
//        model.addAttribute("name", name);
        return "confirm";
    }

    @PostMapping(path = {"/order/submit"})
    public String orderSubmit(OrderSubmitRequestVo vo, Model model) {


        OrderSubmitResponseVo responseVo = orderService.orderSubmit(vo);

        if (responseVo.isOk()) {
            model.addAttribute("vo", responseVo);
            return "pay";
        }
//        redirectAttributes.addAttribute("message", responseVo.getErrorCode().getMessage());
//        redirectAttributes.addAttribute("name", "John Doe");
        return OrderConstant.REDIRECT_ORDER_BASE_DOMAIN_URL + "/toTrade";
    }


    @GetMapping("/redirect-example")
    public String redirectExample(RedirectAttributes redirectAttributes) {
        // 添加Flash属性
        redirectAttributes.addFlashAttribute("flashMessage", "This is a flash message");

        // 添加普通属性
        redirectAttributes.addAttribute("name", "John Doe");

        return OrderConstant.REDIRECT_ORDER_BASE_DOMAIN_URL + "/target-view";
    }

    @GetMapping("/target-view")
    @ResponseBody
    public Map targetView(@ModelAttribute("flashMessage") String flashMessage,
                          @RequestParam("name") String name,
                          Model model) {
        HashMap<String, String> map = new HashMap<>();
        // 获取Flash属性
        map.put("flashMessage", flashMessage);

        // 获取普通属性
        map.put("name", name);

        return map;
    }

    @GetMapping("/msg")
    @ResponseBody
    public String testMsgMQ() {

        OrderEntity order = new OrderEntity();
        order.setOrderSn(IdWorker.getTimeId());
        order.setModifyTime(new Date());
        rabbitTemplate.convertAndSend(OrderRabbitMQConfig.ORDER_EVENT_EXCHANGE_NAME, OrderRabbitMQConfig.ORDER_CREATE_ROUTE_KEY_NAME, order);
        log.info("send msg success :{}", order);
        return "ok";
    }

    @Autowired
    RabbitTemplate rabbitTemplate;

}
